R Matrix Exercises - Solutions

Through these exercises we will review the matrix data structure and perhaps introduce you to a few ideas for you to discover on your own! Just answer the questions below written in bold:

Ex 1: Create 2 vectors A and B, where A is (1,2,3) and B is (4,5,6). With these vectors, use the cbind() or rbind() function to create a 2 by 3 matrix from the vectors. You'll need to figure out which of these binding functions is the correct choice.

In [5]:
A <- c(1,2,3)
B <- c(4,5,6)
rbind(A,B)
Out[5]:
A123
B456

Ex 2: Create a 3 by 3 matrix consisting of the numbers 1-9. Create this matrix using the shortcut 1:9 and by specifying the nrow argument in the matrix() function call. Assign this matrix to the variable mat

In [6]:
mat <- matrix(1:9,byrow=TRUE,nrow = 3)

Ex 3: Confirm that mat is a matrix using is.matrix()

In [7]:
is.matrix(mat)
Out[7]:
TRUE

Ex 4: Create a 5 by 5 matrix consisting of the numbers 1-25 and assign it to the variable mat2. The top row should be the numbers 1-5.

In [11]:
mat2 <- matrix(1:25,byrow = TRUE,nrow = 5)
mat2
Out[11]:
12345
6 7 8 910
1112131415
1617181920
2122232425

Ex 5: Using indexing notation, grab a sub-section of mat2 from the previous exercise that looks like this:

[7,8]
[12,13]
In [12]:
mat2[2:3,2:3]
Out[12]:
78
1213

Ex 6: Using indexing notation, grab a sub-section of mat2 from the previous exercise that looks like this:

[19,20]
[24,25]
In [17]:
mat2[4:5,4:5]
Out[17]:
1920
2425

Ex 7: What is the sum of all the elements in mat2?

In [18]:
sum(mat2)
Out[18]:
325

Ex 8: Ok time for our last exercise! Find out how to use runif() to create a 4 by 5 matrix consisting of 20 random numbers (4*5=20).

In [25]:
ranmat <- matrix(runif(20,min=0,max=100),nrow = 4)
ranmat
Out[25]:
21.0399641.2168951.4671635.2445165.69687
60.81269681.32660681.54530094.459102 3.403767
72.1324132.4819418.7460035.1630390.70033
1.79834535.55823394.30099117.99132011.074018
In [21]:
help(runif)
Out[21]:
Uniform {stats}R Documentation

The Uniform Distribution

Description

These functions provide information about the uniform distribution on the interval from min to max. dunif gives the density, punif gives the distribution function qunif gives the quantile function and runif generates random deviates.

Usage

dunif(x, min = 0, max = 1, log = FALSE)
punif(q, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE)
qunif(p, min = 0, max = 1, lower.tail = TRUE, log.p = FALSE)
runif(n, min = 0, max = 1)

Arguments

x, q

vector of quantiles.

p

vector of probabilities.

n

number of observations. If length(n) > 1, the length is taken to be the number required.

min, max

lower and upper limits of the distribution. Must be finite.

log, log.p

logical; if TRUE, probabilities p are given as log(p).

lower.tail

logical; if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].

Details

If min or max are not specified they assume the default values of 0 and 1 respectively.

The uniform distribution has density

f(x) = 1/(max-min)

for min ≤ x ≤ max.

For the case of u := min == max, the limit case of X == u is assumed, although there is no density in that case and dunif will return NaN (the error condition).

runif will not generate either of the extreme values unless max = min or max-min is small compared to min, and in particular not for the default arguments.

Value

dunif gives the density, punif gives the distribution function, qunif gives the quantile function, and runif generates random deviates.

The length of the result is determined by n for runif, and is the maximum of the lengths of the numerical arguments for the other functions.

The numerical arguments other than n are recycled to the length of the result. Only the first elements of the logical arguments are used.

Note

The characteristics of output from pseudo-random number generators (such as precision and periodicity) vary widely. See .Random.seed for more information on R's random number generation algorithms.

References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

See Also

RNG about random number generation in R.

Distributions for other standard distributions.

Examples

u <- runif(20)

## The following relations always hold :
punif(u) == u
dunif(u) == 1

var(runif(10000))  #- ~ = 1/12 = .08333

[Package stats version 3.2.2 ]

Great Job!